package cc.smarnet.caihou.phone.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;

import cc.smarnet.caihou.IPConfig;
import cc.smarnet.caihou.LoginActivity;
import cc.smarnet.caihou.R;

public class UserFragment extends Fragment {
    private View view;
    public static final int MODE_PRIVATE = 0x0000;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_user, container, false);

        @SuppressLint("WrongConstant") SharedPreferences sp = getActivity().getSharedPreferences("userInfo", MODE_PRIVATE);
        String userInfo = sp.getString("userInfo", null);


        TextView user = view.findViewById(R.id.user);
        TextView version = view.findViewById(R.id.version);
        TextView baseserver = view.findViewById(R.id.baseserver);
        user.setText("当前登录人："+ JSON.parseObject(userInfo).getString("realname"));
        try {
            version.setText("当前版本："+ getapkname());
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseserver.setText("当前服务器："+ IPConfig.baseUrl.substring(0,14));
        //退出登录

        Button logout_Button = view.findViewById(R.id.logout_Button);
        logout_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                System.out.println("-----------退出登录--------------------");

                Intent intent = new Intent(getActivity(), LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)  ; //它可以关掉所要到的界面中间的activity
                startActivity(intent);

//                AlertDialog logoutDialog =new AlertDialog.Builder(android.content.Context.c)
//                        .setTitle("提示")
//                        .setMessage("确定要退出登录吗")
//                        .setPositiveButton("确定",null)
//                        .create();
//                logoutDialog.show();
            }
        });
        return view;
    }

    private String getapkname() throws Exception {

// 获取packagemanager的实例

        PackageManager packagemanager = getActivity().getPackageManager();

// getpackagename()是你当前类的包名，0代表是获取版本信息

        PackageInfo packinfo = packagemanager.getPackageInfo(getActivity().getPackageName(),  0);

        String version = packinfo.versionName;

        return version;

    }
 
}
